package com.chuangjiangx.member.coupon.ddd.domain.model;

import java.util.ArrayList;
import java.util.List;

/**
 * 领取方式枚举
 *
 * @author wp
 * @date 2018-08-31 15:54
 **/
public enum GetChannelEnum {
    WX_PUSH("微信推送", 1),
    PAY_SUCCESS_PAGE("支付成功页面", 2);

    public String name;
    public Integer code;

    GetChannelEnum(String name, Integer code) {
        this.name = name;
        this.code = code;
    }

    public static GetChannelEnum fromCode(Integer code) {
        for (GetChannelEnum getChannelEnum : GetChannelEnum.values()) {
            if (code.equals(getChannelEnum.code)) {
                return getChannelEnum;
            }
        }
        throw new IllegalStateException("不存在 该选项:  " + code);
    }

    /**
     * jsonString 转list
     *
     * @param param
     * @return
     */
    public static List<GetChannelEnum> fromCodeList(String param) {
        String[] codeList = param.split(",");
        List<GetChannelEnum> getChannelListEnum = new ArrayList<>();
        for(String code:codeList){
            GetChannelEnum getChannelEnum = fromCode(Integer.valueOf(code));
            getChannelListEnum.add(getChannelEnum);
        }
        return getChannelListEnum;
    }

    /**
     * list 转jsonString
     *
     * @param getChannelEnumList
     * @return
     */
    public static String toJsonList(List<GetChannelEnum> getChannelEnumList) {
        StringBuilder codeList = new StringBuilder();
        getChannelEnumList.forEach(getChannelEnum -> {
            codeList.append(getChannelEnum.code);
            codeList.append(",");
        });
        if(codeList.length()>0){
            codeList.deleteCharAt(codeList.lastIndexOf(","));
        }
        return codeList.toString();
    }
}
